package com.yicong.ants.ui.common;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;

import com.yicong.ants.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * 抽象的，精简的 显示 title 的adapter, 如果需要懒加载 就需要 复写 instantiateItem 方法
 *
 * @author cchao
 * @version 2019-07-20.
 */
public class AbstractViewPagerAdapter extends PagerAdapter {

    private List<View> mViews = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();


    public AbstractViewPagerAdapter(List<String> mTitles) {
        super();
        this.mTitles.addAll(mTitles);
    }

    public AbstractViewPagerAdapter(List<View> views, List<String> mTitles) {
        super();
        this.mViews.addAll(views);
        this.mTitles.addAll(mTitles);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return null != mTitles ? mTitles.get(position) : Constants.NO_NULL;
    }

    @Override
    public int getCount() {
        return mTitles.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mViews.isEmpty()) {
            return null;
        }
        container.addView(mViews.get(position));
        return mViews.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
